package com.test.testCode.insertphone.service;

import com.test.testCode.insertphone.entity.SysStudTelbook;
import com.test.testCode.insertphone.repository.StudTelBookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class StudTelBookService {
    @Autowired
    private StudTelBookRepository studTelBookRepository;

    public int addTelBook() {
        List<Map<String, String>> list = studTelBookRepository.get();
        List<SysStudTelbook> books = new ArrayList<>();
        SysStudTelbook book = null;
        for (Map<String, String> map : list) {
            book = new SysStudTelbook();
            book.setTelId(String.valueOf(nextId()));
            book.setStudentCode(map.get("student_code"));
            book.setStudentId(map.get("student_id"));
            book.setIsWhite("1");
            book.setIsAllow("1");
            book.setStatus("1");
            book.setTel("84710771");
            book.setTelName("售后服务");
            books.add(book);
        }
        studTelBookRepository.saveAll(books);
        return books.size();
    }


    private long lastTimestamp = -1L;
    private final long sequenceBits = 12L;
    private final long sequenceMask = -1L ^ (-1L << sequenceBits);
    private long sequence = 0L;
    private final long twepoch = 1420041600000L;
    private final long workerIdBits = 5L;
    /** 数据标识id所占的位数 */
    private final long datacenterIdBits = 5L;
    private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
    private long datacenterId;
    private final long workerIdShift = sequenceBits;
    private final long datacenterIdShift = sequenceBits + workerIdBits;
    private long workerId;
    public synchronized long nextId() {
        long timestamp = System.currentTimeMillis();

        //如果当前时间小于上一次ID生成的时间戳，说明系统时钟回退过这个时候应当抛出异常
        if (timestamp < lastTimestamp) {
            throw new RuntimeException(
                    String.format("Clock moved backwards.  Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
        }

        //如果是同一时间生成的，则进行毫秒内序列
        if (lastTimestamp == timestamp) {
            sequence = (sequence + 1) & sequenceMask;
            //毫秒内序列溢出
            if (sequence == 0) {
                //阻塞到下一个毫秒,获得新的时间戳
                timestamp = tilNextMillis(lastTimestamp);
            }
        }
        //时间戳改变，毫秒内序列重置
        else {
            sequence = 0L;
        }

        //上次生成ID的时间截
        lastTimestamp = timestamp;

        //移位并通过或运算拼到一起组成64位的ID
        return ((timestamp - twepoch) << timestampLeftShift) //
                | (datacenterId << datacenterIdShift) //
                | (workerId << workerIdShift) //
                | sequence;
    }

    protected long tilNextMillis(long lastTimestamp) {
        long timestamp = System.currentTimeMillis();
        while (timestamp <= lastTimestamp) {
            timestamp = System.currentTimeMillis();
        }
        return timestamp;
    }
}
